package com.zygh.lz.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.PoliceData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface PoliceDataMapper extends BaseMapper<PoliceData> {

    @Select("<script> " +
            "SELECT p.jjsj01,p.jjyxm110,p.jqdz110,(CASE \n" +
            "WHEN (ISNULL(p.cjxq)=1) || (LENGTH(trim(p.cjxq))=0)  \n" +
            "THEN  '未反馈'\n" +
            "ELSE  '已反馈'\n" +
            "END\n" +
            ") AS cjxq,p.zpcjr,p.czhr,p.cjsj,p.bjnr,t.name," +
            "sc.section_name  sectionname,p.czhdw,p.cjbs " +
            "FROM police_data p LEFT JOIN police_type t ON p.cjlb = t.cjlb\n" +
            "LEFT JOIN section sc ON p.czhdw = sc.tempid  \n" +
            "<where> " +
            "p.jjsj01 BETWEEN #{startTime} AND #{endTime} and IFNULL(p.zpcjr,'') != 'unknown'  " +
            " <if test = 'sectionTel != null'>" +
            "   AND czhdw = #{sectionTel} " +
            " </if>" +
            "AND sc.section_name IS NOT NULL   " +
            "</where>" +
            "ORDER BY p.jjsj01 DESC " +
            "</script>")
    List<Map> findPoliceData(@Param("sectionTel") String sectionTel,
                             @Param("startTime") String startTime,
                             @Param("endTime") String endTime);

    @Select("<script> " +
            "SELECT count(*) " +
            "FROM police_data \n" +
            "<where> " +
            "jjsj01 BETWEEN #{startTime} AND #{endTime} " +
            " <if test = 'sectionTel != null'>" +
            "   AND czhdw = #{sectionTel} " +
            " </if>" +
            " <if test = 'type != null'>" +
            "   AND bjnr like concat(concat('%',#{type}),'%')  " +
            " </if>" +
            "</where>" +
            "</script>")
    Integer accident(@Param("sectionTel") String sectionTel,
                     @Param("startTime") String startTime,
                     @Param("endTime") String endTime,
                     @Param("type") String type);


    //有则更新，无则插入
    int UpdateInsertPolice(PoliceData policeData);

    List<Map<String, Object>> findalertInfo(@Param("sectionTel") String sectionTel,
                                            @Param("startTime") String startTime,
                                            @Param("endTime") String endTime);

    //接处警个人排名
    List<Map<String, Object>> findPoliceDateRanking(@Param("code") String code,
                                                    @Param("startTime") String startTime,
                                                    @Param("endTime") String endTime);

    //接处警前三名
    List<Map<String, Object>> findPoliceDateTop3(@Param("startTime") String startTime,
                                                 @Param("endTime") String endTime);


}